package com.one.common.utils;

import android.app.Activity;

import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.util.Map;

/**
 * Author: zhulunjun
 * Time  : 2018/10/24 11:10
 * email : zhulunjun@qq.com
 * Describe : 友盟第三方登录工具类
 **/
public class UMAuthUtils implements UMAuthListener {
    public static UMAuthUtils instance;
    public static synchronized UMAuthUtils getInstance() {
        if (instance == null) {
            instance = new UMAuthUtils();
        }
        return instance;
    }

    /**
     * 第三方登录
     */
    public void auth(Activity context, SHARE_MEDIA media, OnAuthListener onAuthListener){
        this.onAuthListener = onAuthListener;
        UMShareAPI.get(context).getPlatformInfo(context, media, this);
    }



    @Override
    public void onStart(SHARE_MEDIA share_media) {
        Logger.d("UMAuthUtils",share_media+"开始登录");
    }

    @Override
    public void onComplete(SHARE_MEDIA share_media, int i, Map<String, String> map) {
        Logger.d("UMAuthUtils",share_media+"授权成功");
        if(onAuthListener != null){
            onAuthListener.onAuth(map);
        }
        Toaster.showLongToast("授权成功");
    }

    @Override
    public void onError(SHARE_MEDIA share_media, int i, Throwable throwable) {
        Logger.d("UMAuthUtils",share_media+"授权失败 "+throwable.getMessage());
        Toaster.showLongToast("授权失败");
    }

    @Override
    public void onCancel(SHARE_MEDIA share_media, int i) {
        Logger.d("UMAuthUtils",share_media+"取消授权");
        Toaster.showLongToast("取消授权");
    }


    /**
     * 授权监听
     */
    public interface OnAuthListener{

        /**
         * map字段
         *
         * uid 用户唯一标识
         *
         * name 用户昵称
         *
         * gender 用户性别 该字段会直接返回男女
         *
         * iconurl 用户头像
         */
        void onAuth(Map<String, String> map);
    }

    private OnAuthListener onAuthListener;

}
